var cache = require("../../common/Cache.js").cache;
var ArticleTypeCache = require("../../common/ArticleTypeCache.js").AtTypeCache;
var dataFilter = require("../../common/DataFilter.js").dataFilter;
var wxParse = require("../../plugs/wxParse/wxParse.js");
var tools = require("../../common/Tools.js").tools; 
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var typeId=options.typeId;
    var that = this;
 
    //typeId = 350;
    
    var data = ArticleTypeCache.getByTypeId(typeId);
    console.log("标题是：", data.Title);
    wx.setNavigationBarTitle({
      title: data.Title
    })   

    app.console.log(data);
     /*tab组件绑定*/
    var c_tab = this.selectComponent("#c_tab");
    this.c_tab = c_tab;
  
    if (data.Line.length==0){
      data.Line.push({
        ID: data.ID,
        Title: "",
        Type: ""
      });
      if (data.Type=="Url"){
        data.Line[0].Type = "Url";
      }
      else if (data.Type=="List"){
        data.Line[0].Type="List";
      }
    }


    if (data.Line!=undefined&&data.Line.length>0){
      var articleData = data;
      app.console.log(data);

      c_tab.setData({
        bindData: articleData.Line
      });

      var swiperPlaneData=[];
      for (var index = 0; index < data.Line.length;index++){
        swiperPlaneData.push({
          "planeIndex":index,
          "planeType":data.Line[index].Type,
          "planeDataId":data.Line[index].ID,
          "hasRender":false
        });
        if (data.Line[index].Type=="Url"){
          swiperPlaneData[index]["planeId"] = "article_"+index;
        } else if (data.Line[index].Type=="List"){
          swiperPlaneData[index]["planeId"] = "articleList_"+index;
        }
      }

      this.setData({
        swiperPlaneData: swiperPlaneData
      });
      
      console.log("数组是", data.Line);

      that._setContent(0, data.Line[0].ID);
    
      /*绑定tab事件 */
      c_tab.ontabTap(function (event) {
        var index = event.currentTarget.dataset.index;
        var id=event.currentTarget.dataset.id;
        
        that._setContent(index, id, swiperPlaneData);

      });
    }else{
      //var articleList = cache.getSync("allArticle");
      //var targetArticle = dataFilter.getFilter("Type", id.toString(), articleList);



    }

  },

  /*设置tab对应内容方法 */
  _setContent: function (index, id){
    var swiperPlaneData = this.data.swiperPlaneData;
    var articleList = cache.getSync("allArticle");
    var targetArticle = dataFilter.getFilter("Type", id.toString(), articleList);
    
    
    // var targetArticle = dataFilter.getFilter("Type", id.toString(), articleList);
    var targetPlane = swiperPlaneData[index];
    app.console.log(this.data);
    if (swiperPlaneData[index]["hasRender"] === false) {
    if (targetPlane.planeType == "List") {
         var c_articleList = this.selectComponent("#" + targetPlane.planeId);
         c_articleList.setData({
           list: targetArticle
           });

          /*绑定列表点击事件*/
           c_articleList.onArticleTap(function (event) {
             var id = event.currentTarget.dataset.id;
             console.log('event=',event)
             wx.navigateTo({
               url: '/pages/ArticleDetail/ArticleDetail?id=' + id,
             })
           });

        } else if (targetPlane.planeType == "Url") {
          
          wxParse.wxParse('article', 'html', tools.clearInvalidStyle(targetArticle[0].ArContent), this, 5, index);
        }
     
        swiperPlaneData[index]["hasRender"]=true;
        
        this.setData({
          "swiperPlaneData": swiperPlaneData
        });
        
       }


    console.log("debug here");
    //wxParse.wxParse('article', 'html', articleDetail.ArContent, that, 5);
    this.setData({
      "swiperIndex": index,
    });
  },

  /*绑定swiper切换方法*/
  swiperChange:function(event){
    if(event.type!=undefined){
      var currentIndex = event.detail.current;
      this.c_tab.setTab(currentIndex);
      this._setContent(currentIndex, this.data.swiperPlaneData[currentIndex].planeDataId)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})